using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;

namespace DotNetNuke.Modules.ActiveForums.Queue
{
	public class Controller
	{
		public static void Add(string EmailFrom, string EmailTo, string EmailSubject, string EmailBody, string EmailBodyPlainText, string EmailCC, string EmailBCC)
		{
			try
			{
				DataProvider.Instance().Queue_Add(EmailFrom, EmailTo, EmailSubject, EmailBody, EmailBodyPlainText, EmailCC, EmailBCC);
			}
			catch (Exception ex)
			{
				DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
			}

		}
	}
	public class Scheduler : DotNetNuke.Services.Scheduling.SchedulerClient
	{

		private string SMTPServer;
		private string SmtpUserName;
		private string SMTPPassword;
		private string SMTPAuthentication;

		public Scheduler(DotNetNuke.Services.Scheduling.ScheduleHistoryItem objScheduleHistoryItem) : base()
		{
			this.ScheduleHistoryItem = objScheduleHistoryItem;
		}
		public override void DoWork()
		{
			try
			{
				int intQueueCount = 0;
				intQueueCount = ProcessQueue();
				ScheduleHistoryItem.Succeeded = true;
				ScheduleHistoryItem.AddLogNote("Processed " + intQueueCount.ToString() + " messages");
			}
			catch (Exception ex)
			{
				ScheduleHistoryItem.Succeeded = false;
				ScheduleHistoryItem.AddLogNote("Process Queue Failed. " + ex.ToString());
				Errored(ref ex);
				DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
			}
		}
		private int ProcessQueue()
		{
			int intQueueCount = 0;
			try
			{
				//Get Host SMTP Settings
				Hashtable objHost = DotNetNuke.Entities.Portals.PortalSettings.GetHostSettings();
				//Get Queue

				IDataReader dr = DataProvider.Instance().Queue_List();
				while (dr.Read())
				{
					intQueueCount += 1;
					Message objEmail = new Message();
					objEmail.Subject = dr["EmailSubject"].ToString();
					objEmail.SendFrom = dr["EmailFrom"].ToString();
					objEmail.SendTo = dr["EmailTo"].ToString();
					objEmail.Body = dr["EmailBody"].ToString();
					objEmail.BodyText = dr["EmailBodyPlainText"].ToString();
					objEmail.SmtpServer = objHost["SMTPServer"].ToString();
					objEmail.SmtpUserName = objHost["SMTPUsername"].ToString();
					objEmail.SmtpPassword = objHost["SMTPPassword"].ToString();
					objEmail.SmtpAuthentication = objHost["SMTPAuthentication"].ToString();
					objEmail.SmtpSSL = objHost["SMTPEnableSSL"].ToString();

					bool canDelete = objEmail.SendMail();
					if (canDelete)
					{
						try
						{
							DataProvider.Instance().Queue_Delete(Convert.ToInt32(dr["Id"]));
						}
						catch (Exception ex)
						{
							DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
						}
					}
					else
					{
						intQueueCount = intQueueCount - 1;
					}
				}
				dr.Close();
				dr.Dispose();

				return intQueueCount;
			}
			catch (Exception ex)
			{
				DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
				return -1;
			}

		}

	}
	public class Message
	{
		public string Subject;
		public string SendFrom;
		public string SendTo;
		public string Body;
		public string BodyText;
		//Public Recipients As ArrayList
		public string SmtpServer;
		public string SmtpUserName;
		public string SmtpPassword;
		public string SmtpAuthentication;
		public string SmtpSSL;
		//Public Subs As List(Of SubscriptionInfo)


		public bool SendMail()
		{
			try
			{
				Hashtable objHost = null;
				if (SmtpServer == "")
				{
					objHost = DotNetNuke.Entities.Portals.PortalSettings.GetHostSettings();
					SmtpServer = Convert.ToString(objHost["SMTPServer"]);
					SmtpUserName = Convert.ToString(objHost["SMTPUsername"]);
					SmtpPassword = Convert.ToString(objHost["SMTPPassword"]);
					SmtpAuthentication = Convert.ToString(objHost["SMTPAuthentication"]);
					SmtpSSL = Convert.ToString(objHost["SMTPEnableSSL"]);
				}
				//Dim Email As New System.Net.Mail.MailMessage

				//Email.From = New System.Net.Mail.MailAddress(SendFrom)
				//Email.To.Add(New System.Net.Mail.MailAddress(SendTo))
				//Email.Subject = Subject
				//Email.IsBodyHtml = True
				//Email.Body = Body
				List<SubscriptionInfo> subs = new List<SubscriptionInfo>();
				SubscriptionInfo si = new SubscriptionInfo();
				si.Email = SendTo;
				si.DisplayName = string.Empty;
				si.LastName = string.Empty;
				si.FirstName = string.Empty;
				subs.Add(si);
				Email oEmail = new Email();
				oEmail.UseQueue = false;
				oEmail.Recipients = subs;
				oEmail.Subject = Subject;
				oEmail.From = SendFrom;
				oEmail.BodyText = BodyText;
				oEmail.BodyHTML = Body;
				oEmail.SmtpServer = SmtpServer;
				oEmail.SmtpUserName = SmtpUserName;
				oEmail.SmtpPassword = SmtpPassword;
				oEmail.SmtpAuthentication = SmtpAuthentication;
				oEmail.SmtpSSL = SmtpSSL;
				try
				{
					System.Threading.Thread objThread = new System.Threading.Thread(oEmail.Send);
					objThread.Start();
					return true;
				}
				catch (Exception ex)
				{
					DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
					return false;
				}
			}
			catch (Exception ex)
			{
				DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
				return false;
			}
		}
	}

}
